Specific use of np. where of [0] and np. where of [1]

  • 2021-09-20 21:08:11
  • OfStack

This article mainly introduces the specific use of np. where () [0] and np. where () [1], and the specific use of np. where (). Needless to say, the details are as follows:


import numpy as np
 
a = np.arange(12).reshape(3,4)
print('a:', a)
print('np.where(a > 5):', np.where(a > 5))
print('a[np.where(a > 5)]:', a[np.where(a > 5)])
print('np.where(a > 5)[0]:', np.where(a > 5)[0])
print('np.where(a > 5)[1]:', np.where(a > 5)[1])
print(a[np.where(a > 5)[0], np.where(a > 5)[1]])

a: [[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]]
np.where(a > 5): (array([1, 1, 2, 2, 2, 2]), array([2, 3, 0, 1, 2, 3]))
a[np.where(a > 5)]: [ 6 7 8 9 10 11]
np.where(a > 5)[0]: [1 1 2 2 2 2]
np.where(a > 5)[1]: [2 3 0 1 2 3]
[ 6 7 8 9 10 11]

np. where () [0] for row index, np. where () [1] for column index

numpy. where () has two uses:

1. np.where(condition, x, y)

The condition (condition) is satisfied, and the output x is not satisfied, but the output y is not satisfied.

If it is a 1-dimensional array, it is equivalent to [xv if c else yv for (c, xv, yv) in zip (condition, x, y)]


>>> aa = np.arange(10)
>>> np.where(aa,1,-1)
array([-1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) # 0 For False , so the first 1 Output -1
>>> np.where(aa > 5,1,-1)
array([-1, -1, -1, -1, -1, -1, 1, 1, 1, 1])

>>> np.where([[True,False], [True,True]],  #  Examples on official website 
  [[1,2], [3,4]],
       [[9,8], [7,6]])
array([[1, 8],
  [3, 4]])

The conditions of the above example are [[True, False], [True, False]], which correspond to the four values of the final output result respectively. The first value is selected from [1, 9], because the condition is True, so it is selected as 1. The second value is selected from [2, 8], because the condition is False, so choose 8, and so on. We can look at another example of similar problems:


>>> a = 10
>>> np.where([[a > 5,a < 5], [a == 10,a == 7]],
       [["chosen","not chosen"], ["chosen","not chosen"]],
       [["not chosen","chosen"], ["not chosen","chosen"]])

array([['chosen', 'chosen'],
    ['chosen', 'chosen']], dtype='<U10')

2. np.where(condition)

Only the condition (condition), without x and y, the coordinates of the elements (equivalent to numpy. nonzero) that meet the condition (i.e. non-0) are output. The coordinates here are given in the form of tuple. Usually, how many dimensions does the original array have, and the output tuple contains several arrays, which correspond to the coordinates of each dimension of the qualified elements respectively.


>>> a = np.array([2,4,6,8,10])
>>> np.where(a > 5)  #  Return index 
(array([2, 3, 4]),)  
>>> a[np.where(a > 5)]   #  Equivalent to  a[a>5]
array([ 6, 8, 10])

>>> np.where([[0, 1], [1, 0]])
(array([0, 1]), array([1, 0]))

In the above example condition, the true values of [[0, 1], [1, 0] are two ones, each with the first dimensional coordinates of [0, 1] and the second dimensional coordinates of [1, 0].

Here's a more complicated example:


>>> a = np.arange(27).reshape(3,3,3)
>>> a
array([[[ 0, 1, 2],
    [ 3, 4, 5],
    [ 6, 7, 8]],

    [[ 9, 10, 11],
    [12, 13, 14],
    [15, 16, 17]],

    [[18, 19, 20],
    [21, 22, 23],
    [24, 25, 26]]])

>>> np.where(a > 5)
(array([0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2]),
 array([2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2]),
 array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]))
#  The eligible elements are 
  [ 6, 7, 8]],

   [[ 9, 10, 11],
    [12, 13, 14],
    [15, 16, 17]],

   [[18, 19, 20],
    [21, 22, 23],
    [24, 25, 26]]]

So np. where outputs the coordinates for each element, because the original array is 3-dimensional, so there are 3 arrays in tuple.

One point to note is that the input cannot be list directly, but needs to be converted to array or array. For example, range (10) and np. arange (10). The latter returns arrays, and np. where is used to achieve results.


Related articles: